package Test;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class Json {
	private final static String text = "{\"pwd\":123,\"username\":\"yb\"}";  
	
	private final static String text_array = "[{\"pwd\":\"123\",\"username\":\"yb\"},{\"pwd\":\"123\",\"username\":\"yb\"}]";
	
	private final static  User user = new User("yb", "123");

	public static void main(String[] args) {
		
		//System.out.println(toJsonString(user));
		
		//System.out.println(parseJsonObject(text));
		
		//System.out.println(parseBeanObject(text).toString());
		
//		System.out.println(jsonArrayToList(text_array));
		
//		System.out.println(jsonArrayToListString(text_array));
		
		//System.out.println(jsonArrayToListMap(text_array));
		
		//System.out.println(bean2Json(user));
		
		//parseJSONAndBeanEachother(user);
		
		//System.out.println(listToJson(jsonArrayToList(text_array)));
		
		//getJson(text, text_array);
		
		//getKey(text_array);
		
		//getEntrykey(text);
		
		//-----------------------------------------------------------------分割线-----------------------------------------------------------------------------

		
	}
	/**
	 * ---------------------------------------------------------com.alibaba.fastion.JSONObject包----------------------------------------------------------------
	 * 
	 */
	/**
	 * 区分是JSONObject还是JSONArray
	 * @param json
	 */
	public static void Distinguish(String json){
		 final char[] strChar = json.substring(0, 1).toCharArray(); 
         final char firstChar = strChar[0];
         if(firstChar == '{'){
        	
         }else if(firstChar == '['){
        	 
         }
		
	}
	
	/**
	 * 序列化(对象转为json字符串)  可以使javabean也可以使json对象,转换成字符串都用这个
	 * @param user
	 * @return
	 */
	public static String toJsonString(User user){
		return JSON.toJSONString(user);
		
	}
	 /** 
     * json字符串反序列化为json对象 （不包含jsonarray)
     */  
    public static JSONObject parseJsonObject(String text){
        JSONObject json = JSON.parseObject(text);  
        return json;
    }  
    /** 
     * json字符串反序列化为javaBean对象 
     */  
    public static User parseBeanObject(String text){  
        User user =  JSON.parseObject(text, User.class);  
        return user;
    }  
    /**
     * jsonArray字符串转换为List<javaBean>
     * 
     */
    public static List<User> jsonArrayToList(String text){  
        List<User> user = JSON.parseArray(text, User.class);  
        return user;
    }
    /**
     * jsonArray字符串转化成List<String>对象
     */
    public static List<String> jsonArrayToListString(String text){
    	List<String> s = JSON.parseObject(text, new TypeReference<List<String>>(){}); 
    	return s;
    }
    /**
     * jsonArray字符串转化成List<Map<String,Object>>对象
     */
    public static List<Map<String,Object>> jsonArrayToListMap(String text){
    	List<Map<String,Object>> list2 = JSON.parseObject(text,new TypeReference<List<Map<String,Object>>>(){});
    	return list2;
    }
    /** 
     * 将javaBean转化为json对象 
     */  
    public static JSONObject bean2Json(User user) {  
        JSONObject jsonObj = (JSONObject) JSON.toJSON(user);  
        return jsonObj;
    }  
    /**
     * 将list转换为jsonarray
     * @param user
     * @return
     */
    public static JSONArray listToJson(List<User> user) {  
        JSONArray jsonObj = (JSONArray) JSON.toJSON(user);  
        return jsonObj;
    } 
    /** 
     * 全序列化 直接把java bean序列化为json文本之后，能够按照原来的类型反序列化回来。支持全序列化，需要打开SerializerFeature.WriteClassName特性 
     * 可以实现返回值null的都显示，不隐藏
     */  
    public static void parseJSONAndBeanEachother(User user) {  
        SerializerFeature[] featureArr = { SerializerFeature.WriteClassName };  
        String text = JSON.toJSONString(user, featureArr);  //user可以是javabean，也可以是JSONObject
        System.out.println(text);  
        User userObj = (User) JSON.parse(text);  //也可以用(User)JSON.parseObject(text,User.class);
        System.out.println(userObj.toString());  
    }  
    
    /**
     * 解析json对象
     */
    public static void getJson(String text, String text_array){
    	JSONObject obj = JSON.parseObject(text);
    	System.out.println(obj.getString("username")+", "+obj.getInteger("pwd"));//obj.getJSONArray(key)和obj.getJSONObject(key)也可以get出来
    	
    	JSONArray array = JSON.parseArray(text_array);
    	for(int i =0; i<array.size();i++){
    		JSONObject temp = array.getJSONObject(i);//当然也可以getJSONArray(i)
    		System.out.println(temp.getString("username")+"------"+temp.getInteger("pwd"));
    	}
    	
    	
    	
    	
    }
    /**
     * 遍历json中所有的key(jsonArray无法遍历)使用keyset()
     */
    public static void getKey(String text){
    	final char[] strChar = text.substring(0, 1).toCharArray(); 
        final char firstChar = strChar[0];
        if(firstChar == '{'){
        	JSONObject jsonObj = JSON.parseObject(text);
        	Set<String> set = jsonObj.keySet();
        	for(String s : set){
        		System.out.println(s);
        	}
        }else if(firstChar == '['){
        	JSONArray array = JSON.parseArray(text);
        	for(int i =0; i<array.size();i++){
        		JSONObject temp = array.getJSONObject(i);//当然也可以getJSONArray(i)
        	    for(String s : temp.keySet()){
        	    	System.out.println(s);
        	    }
        	    
        	}
        }
    	
    }
    
    public static void getEntrykey(String text){
    	final char[] strChar = text.substring(0, 1).toCharArray(); 
        final char firstChar = strChar[0];
        if(firstChar == '{'){
        	JSONObject jsonObj = JSON.parseObject(text);
        	for (Map.Entry<String, Object> entry : jsonObj.entrySet()) {
                System.out.println(entry.getKey() + ":" + entry.getValue());
            }
        }else if(firstChar == '['){
        	JSONArray array = JSON.parseArray(text);
        	for(int i =0; i<array.size();i++){
        		JSONObject jsonObj = array.getJSONObject(i);//当然也可以getJSONArray(i)
        		for (Map.Entry<String, Object> entry : jsonObj.entrySet()) {
                    System.out.println(entry.getKey() + ":" + entry.getValue());
                }
        	    
        	}
        }
    	
    }
    
    
 
	

}
